package com.club.football.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.club.football.common.UserHolder;
import com.club.football.dto.PageDto;
import com.club.football.dto.ScheduleSaveDto;
import com.club.football.entity.Schedule;
import com.club.football.entity.Team;
import com.club.football.mapper.ScheduleMapper;
import com.club.football.req.SchedulePageReq;
import com.club.football.service.ScheduleService;
import com.club.football.service.TeamService;
import com.club.football.utils.BeanUtils;
import com.club.football.utils.R;
import com.club.football.vo.SchedulePageVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.club.football.common.CommonConstants.*;
import static com.club.football.utils.FileUtils.getUrl;

/**
 * 赛程服务实现
 *
 * @author 1
 * @date 2024/01/09
 */
@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule> implements ScheduleService {
    @Resource
    private TeamService teamService;

    @Override
    public R<Page<SchedulePageVo>> schedulePage(PageDto<Schedule> pageDto, SchedulePageReq schedulePageReq) {
        lambdaQuery().eq(schedulePageReq.getEventsId() != null, Schedule::getEventsId, schedulePageReq.getEventsId())
                .like(StringUtils.isNotBlank(schedulePageReq.getScheduleName()), Schedule::getScheduleName, schedulePageReq.getScheduleName())
                .and(CollUtil.isNotEmpty(schedulePageReq.getTeamIdList()), i -> i.in(Schedule::getHomeTeamId, schedulePageReq.getTeamIdList())
                        .or()
                        .in(Schedule::getVisitingTeamId, schedulePageReq.getTeamIdList()))
                .page(pageDto);
        List<Schedule> records = pageDto.getRecords();
        Page<SchedulePageVo> schedulePage = new Page<>();
        if (CollUtil.isNotEmpty(records)) {
            ArrayList<Long> teamIds = new ArrayList<>();
            for (Schedule item : records) {
                teamIds.add(item.getVisitingTeamId());
                teamIds.add(item.getHomeTeamId());
            }
            Map<Long, String> teamLogoMap = teamService.lambdaQuery().in(Team::getTeamId, teamIds).list()
                    .stream().collect(Collectors.toMap(Team::getTeamId, Team::getTeamLogo));
            List<SchedulePageVo> collect = records.stream().map(item -> {
                SchedulePageVo schedulePageVo = BeanUtils.copier(item, new SchedulePageVo());
                schedulePageVo.setHomeTeamLogo(getUrl(teamLogoMap.get(item.getHomeTeamId())));
                schedulePageVo.setVisitingTeamLogo(getUrl(teamLogoMap.get(item.getVisitingTeamId())));
                return schedulePageVo;
            }).collect(Collectors.toList());
            schedulePage.setRecords(collect);
        }
        schedulePage.setTotal(pageDto.getTotal());
        return R.ok(schedulePage, GET_INFO_OK);
    }


    @Override
    public R<String> scheduleSave(ScheduleSaveDto saveDto) {
        Schedule schedule = BeanUtils.copier(saveDto, new Schedule());

        Team homeTeam = teamService.getById(saveDto.getHomeTeamId());
        Team visitingTeam = teamService.getById(saveDto.getVisitingTeamId());

        schedule.setHomeTeamName(homeTeam.getName());
        schedule.setVisitingTeamName(visitingTeam.getName());

        if (schedule.getHomeTeamScore() > schedule.getVisitingTeamScore()) {
            schedule.setMatchSituation(MATCH_SUCCESS);
        } else if (schedule.getHomeTeamScore() < schedule.getVisitingTeamScore()) {
            schedule.setMatchSituation(MATCH_FILED);
        } else {
            schedule.setMatchSituation(MATCH_A_DRAW);
        }

        Long userId = UserHolder.getUser().getUserId();
        schedule.setUpdateBy(userId);
        if (schedule.getScheduleId() != null) {
            updateById(schedule);
            return R.ok(EDIT_OK_MSG);
        }
        schedule.setCreateBy(userId);
        save(schedule);
        return R.ok(SAVE_OK_MSG);
    }
}
